package q2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientSocketTCPQ2 extends Thread
{
	//	Socket to communicate with the server
	Socket sktOut = null;
	
	//	Soket to receive data from the server
	Socket sktIn = null;

	public ClientSocketTCPQ2(String IP, int iPortIn, int iPortOut )
	{
		start();
		
		System.out.print("\n\nClientSocketTCPQ2 CLIENT INSTANCE\n");

		String strHttpRequest = "GET ./test.html HTTP/1.1\nHost: www.test.net\nConnection: close\nUser-agent: Mozilla/4.0\nAccept-language:fr\n";
		
		try {
			//	Create input and output sockets for client-server communication
			sktIn = new Socket(IP, iPortIn);
			sktOut = new Socket(IP, iPortOut);
			
			//	Create output stream from ouput socket
			PrintWriter out = new PrintWriter( sktOut.getOutputStream(), true );

			//	Create input stream to listen from the server for the response
			BufferedReader in = new BufferedReader( new InputStreamReader(sktIn.getInputStream()));
			
			sleep(1000);
			
			//	Send request to server
			System.out.print("ClientSocketTCPQ2 Sending request: '" + strHttpRequest + "'\n");
			out.print( strHttpRequest );
			out.close();

			while(!in.ready())
			{
				sleep(100);
			}

			System.out.println(in.readLine()); // Read one line and output
			System.out.println();
//			in.close();

		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
